package com.szgr.baoyin.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.DYCardListAdapter;
import com.szgr.baoyin.model.CardType;
import com.szgr.baoyin.model.DYCard;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.RLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DYCardActivity extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    private List<DYCard> data;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 33:
                    listAdapter.setData(data);
                    break;
                case 34:
                    getCard();
                    break;
            }
        }
    };
    private DYCardListAdapter listAdapter;
    private Dialog dialog;
    private DYCard selectCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_dycard2);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("我的都银卡");
    }

    @Override
    public void initView() {
        data = new ArrayList<>();
        ListView cardList = (ListView) findViewById(R.id.card_list);
        listAdapter = new DYCardListAdapter(this, data);
        cardList.setAdapter(listAdapter);
        cardList.setOnItemClickListener(this);
        cardList.setOnItemLongClickListener(this);

        getCard();

        View view = LayoutInflater.from(this).inflate(R.layout.layout_save_goods_dialog,null,false);
        view.findViewById(R.id.save_goods_delete).setOnClickListener(this);
        view.findViewById(R.id.save_goods_cancel).setOnClickListener(this);
        dialog = MyTool.getCenterDialog(this, view);
    }

    private void getCard() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_MY_PROCARD,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("---douyin card--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONArray card_list = datas.getJSONArray("card_list");
                            Gson gson = new Gson();
                            data = gson.fromJson(card_list.toString(),new TypeToken<List<DYCard>>(){}.getType());
                            mHandler.obtainMessage(33).sendToTarget();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key", MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.save_goods_delete:
                dialog.dismiss();
                deleteCard(selectCard.getId());
                break;
            case R.id.save_goods_cancel:
                dialog.dismiss();
                break;
        }
    }

    private void deleteCard(final String id) {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_DELETE_CARD,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("----delete--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            String datas = jsonObject.getString("datas");
                            if("1".equals(datas)){
                                mHandler.obtainMessage(34).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("id",id);
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        DYCard dyCard = data.get(position);
        Intent intent = new Intent(this,DYCardDetailActivity.class);
        intent.putExtra("id",dyCard.getId());
        intent.putExtra("card",dyCard);
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        selectCard = data.get(position);
        dialog.show();
        return true;
    }
}
